################################################
# Prepare CHES Data
################################################

cat("\nPreparing CHES data.")

library(tidyverse)
library(haven)

ches <- read_dta("CHES/ches_2019.dta")

# Country and party name

ches$partyname_ches <- ches$party
ches$country_ches <- ches$country %>% as_factor %>% paste

# Vars

ches$galtan_ches <- ches$galtan %>% as.numeric()
ches$galtan_salience_ches <- ches$galtan_salience %>% as.numeric()
ches$lrecon_ches <- ches$lrecon %>% as.numeric()
ches$lrecon_salience_ches <- ches$lrecon_salience %>% as.numeric()

# IDs

ches$id_ches <- ches$party_id %>% as.numeric()

# Populism

ches$populism_ches <- ches$people_vs_elite %>% as.numeric()

ches_prep <- select(ches,
                     ends_with("_ches", ignore.case = F),
                     starts_with("id_", ignore.case = F)) %>%
  select(country_ches,
         partyname_ches,
         starts_with("id_"),
         everything())

saveRDS(ches_prep, "CHES/ches_prep.rds")

cat("\nCHES data preparation finished.\n")